﻿using IntexReport.DataSource;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Windows;
using System.Windows.Controls;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for SummaryForm.xaml
    /// </summary>
    /// 
    public partial class FLTRMATMOV : DocumentBase
    {
        #region-------------Properties-----------------------
        int ThreadCount = 0;
        int finishedcount = 0;
        int totalrecord = 0;
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        private SetData src
        {
            get
            {
                return (SetData)TryFindResource("dataSource");
            }
        }
        private string LO
        {
            get
            {
                return ((TextBox)FindName("txtlo")).Text.ToString();
            }
        }
        private string LB
        {
            get
            {
                return ((TextBox)FindName("txtlb")).Text.ToString();
            }
        }
        private string ABTYPE
        {
            get
            {
                return ((TextBox)FindName("txtabtype")).Text.ToString();
            }
        }
        FLTRMATMOVData m_Data, m_DataX;
        public DataSet setResult;
        private DataRow dwUI;
        FLTRMATMOVReport m_rpt;
        bool flagstartmtr;
        ExcelX.ExcelLib.ExcelAgent m_exAgent;
        DataSet setANINFOR = new DataSet();
        DataTable tableUI;
        string LASTINVDATE;
        public FLTRMATMOV()
        {
            InitializeComponent();
            this.Name = "原料收发存"; 
        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            try
            {
                base.SetUp(dicParamater);
                this.dicParamater = dicParamater;
                OracleDB = (OracleConnection)dicParamater["OracleDB"];
                proxy = (IntexReptServBLClient)dicParamater["proxy"];
                orlReadCommand = OracleDB.CreateCommand();
                orlReadCommand.Parameters.Add("OrdNum", OracleType.NVarChar).Value = "";
                m_Data = new FLTRMATMOVData(OracleDB, proxy);
         
                tableUI = new DataTable("UIData");
                tableUI.Columns.Add("INVDATE", typeof(string));
                tableUI.Columns.Add("STARTDATE", typeof(string));
                tableUI.Columns.Add("ENDDATE", typeof(string));
                tableUI.Columns.Add("LO", typeof(string));
                tableUI.Columns.Add("LB", typeof(string));
                tableUI.Columns.Add("ABTYPE", typeof(string));
                txtBMONTH.Text = m_Data.Right("00" + DateTime.Now.Month.ToString(), 2);
                txtBYEAR.Text = DateTime.Now.Year.ToString();
                txtBDAY.Text = DateTime.Now.Day.ToString();
                txtEMONTH.Text = m_Data.Right("00" + DateTime.Now.Month.ToString(), 2);
                txtEYEAR.Text = DateTime.Now.Year.ToString();
                txtEDAY.Text = DateTime.Now.Day.ToString();
                flagstartmtr = false;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        #endregion

        private void btnQuery_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                finishedcount = 0;
                src.Clear();
                src.AcceptChanges();
                string bYear = txtBYEAR.Text.ToString();
                string bMonth = m_Data.Right("00" + txtBMONTH.Text.ToString(), 2);
                string bDay = m_Data.Right("00" + txtBDAY.Text.ToString(), 2);
                string eYear = txtEYEAR.Text.ToString();
                string eMonth = m_Data.Right("00" + txtEMONTH.Text.ToString(), 2);
                string eDay = m_Data.Right("00" + txtEDAY.Text.ToString(), 2);

                string startdate = bYear
                    + m_Data.Right("00" + bMonth, 2)
                    + m_Data.Right("00" + bDay.ToString().Trim(), 2);
                string enddate = eYear
                    + m_Data.Right("00" + eMonth, 2)
                    + m_Data.Right("00" + eDay.ToString().Trim(), 2);
                tableUI.Clear();
                dwUI = tableUI.NewRow();
                dwUI["STARTDATE"] = startdate;
                dwUI["ENDDATE"] = enddate;
                dwUI["LO"] = LO;
                dwUI["LB"] = LB;
                dwUI["ABTYPE"] = ABTYPE;                
                tableUI.Rows.Add(dwUI);
                m_Data.flagstartmtr = flagstartmtr;
                using (new LongOperation(this, "读取符合条件的记录"))
                {
                    setResult = m_Data.GetData(dwUI);
                    src.Merge(setResult);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void GenerateReport(string PrintType)
        {
            m_DataX = new FLTRMATMOVData(dicParamater);
            m_DataX.exAgent._app.Visible = false;
            m_DataX.OracleDB = this.OracleDB;
            m_DataX.proxy = this.proxy;
            m_rpt = new FLTRMATMOVReport(dicParamater);
            m_rpt.dsSource = src;
            m_rpt.PrintType = PrintType;
            m_rpt.exAgent = m_DataX.exAgent;
            m_rpt.WriteExcelReport("EXCEL", dwUI["STARTDATE"].ToString(), dwUI["ENDDATE"].ToString());
        }

        private void btnPrint_Click(object sender, RoutedEventArgs e)
        {
            string PrintType = "JustExcel";
            GenerateReport(PrintType);
        }

    }
}
